<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

class Principal extends CI_Controller
{

	public function __construct()
	{
		parent::__construct();
	}

	/**
	 *
	 * Enter description here ...
	 */
	public function index()
	{
		$data = array(
            'title_for_layout' => 'Administra&ccedil;&atilde;o | Not&iacute;cia',
            'title_for_page'   => 'Listagem de Not&iacute;cia',
            'content_layout'   => 'admin/modulos/principal/principal'
        );
        $this->load->view('admin/layout_main', $data);
	}

	/**
	 *
	 * Enter description here ...
	 */
	public function cadastro()
	{
	    $data['title_for_layout'] = 'Administra&ccedil;&atilde;o';
		$data['content_layout']   = 'admin/modulos/noticia/noticia_add';
		$this->parser->parse('admin/layout_main', $data);
	}

	/**
	 * Busca os dados de um registro único do sistema.
	 *
	 * @author Bruno Pulis  bruno.pulis[at]gmail.com
	 * @access public
	 * @param  $id int Chave primária do registro
	 */
	public function alterar($id)
	{
		$this->load->model('admin/noticia_model');
		$data = array(
            'title_for_layout' => 'Administra&ccedil;&atilde;o | Not&iacute;cia',
            'title_for_page'   => 'Editar Not&iacute;cia',
            'dados_categoria'  => $this->noticia_model->alterar($id),
            'content_layout'   => 'admin/noticia_alterar'
         );
         $this->parser->parse('admin/layout_main', $data);
	}

	/**
	 *
	 * Enter description here ...
	 */
	public function gravar_alteracao()
	{
		$this->load->library('form_validation');
		$config = array(
			array(
                'field' => 'titulo',
                'label' => 'Titulo',
                'rules' => 'required|min_length[3]|trim'
            ),
            array(
                'field' => 'subtitulo',
                'label' => 'Subtitulo',
                'rules' => 'required|min_length[3]|trim'
            ),
            array(
                'field' => 'noticia',
                'label' => 'Noticia',
                'rules' => 'required|min_length[100]|max_length[400]|htmlspecialchars'
            ),
            array(
                'field' => 'autor',
                'label' => 'Autor',
                'rules' => 'required|min_length[3]'
            )
       );
      $this->form_validation->set_rules($config);

      if ( $this->form_validation->run() == FALSE )
      {
         $this->alterar($this->input->post('id'));
      }
      else
      {
         $data['id']        = $this->input->post('id');
         $data['titulo']    = $this->input->post('titulo');
         $data['subtitulo'] = $this->input->post('subtitulo');
         $data['noticia']   = $this->input->post('noticia');
         $data['autor']     = $this->input->post('autor');
                 
         $this->load->model('admin/noticia_model');
         if ( $this->noticia_model->gravar_alteracao( $data ) )
         {
             redirect(base_url() . 'admin/noticia/', 'refresh');
         }
         else
         {
             echo "Erro ao alterar notícia";
         }
      }
	}

	/**
	 *
	 * Enter description here ...
	 */
	public function excluir($id)
	{
		$this->load->model('admin/noticia_model');
		if ($this->noticia_model->excluir($id))
		{
			redirect('admin/noticia/', 'refresh');
		}
		else
		{
			echo "Erro ao excluir noticia";
		}
	}

}

/* End of file raca.php */
/* Location: ./application/controller/admin/raca.php */